package com.example.base_lib

import android.os.Bundle
import androidx.annotation.LayoutRes
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding
import androidx.lifecycle.ViewModelProvider
import com.example.base_lib.util.ScreenUtil

abstract class BaseVMActivity<T : ViewDataBinding,VM:BaseViewModel>:AppCompatActivity() {
    lateinit var binding: T
    lateinit var viewModel: VM
    private var hasFullScreen = false
    private val mScreenUtil by lazy { ScreenUtil() }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        if(hasFullScreen){
            mScreenUtil.setFullscreen(this)
        }

        binding = DataBindingUtil.setContentView(this, getLayoutId())
        binding.lifecycleOwner = this
        viewModel = ViewModelProvider(this).get(getViewModelClass())
        ViewCompat.setOnApplyWindowInsetsListener(binding.root) { view, windowInsets ->
            val insets = windowInsets.getInsets(WindowInsetsCompat.Type.statusBars())
            view.setPadding(0, insets.top, 0, mScreenUtil.getNavigationBarHeight(this))
            WindowInsetsCompat.CONSUMED
        }
    }

    /**
     * 例如：R.layout.activity_main
     */
    @LayoutRes
    abstract fun getLayoutId(): Int

    /**
     * 例如：MyViewModel::class.java
     */
    abstract fun getViewModelClass(): Class<VM>  // 声明一个抽象方法来获取 ViewModel 的 Class

    /**
     * 设置全屏
     * 默认非全屏
     * true-全屏
     */
    fun setFullScreen(flag:Boolean){
        hasFullScreen = flag
    }


}